package cn.dnui_xcz826.dnuifood_16110100826.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.dnui_xcz826.dnuifood_16110100826.R;
import cn.dnui_xcz826.dnuifood_16110100826.bean.CommentBean;

public class FoodInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<CommentBean> list;
    private LayoutInflater layoutInflater;

    public FoodInfoAdapter(Context context, List list,int layoutResource) {
        this.context=context;
        this.list=list;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setList(List<CommentBean> list) {
        this.list = list;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = layoutInflater.from(viewGroup.getContext()).inflate(R.layout.foodinfo_comment_item, viewGroup, false);
        return new FoodInfoAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final CommentBean comment = (CommentBean) list.get(position);
        if (comment==null)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        if (comment.getUsername()==null){
            viewHolder.username.setText("null");
        }
        else{
            viewHolder.username.setText(comment.getUsername());
        }
        viewHolder.time.setText(comment.getComment_time());
        viewHolder.content.setText(comment.getContent());
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView username,time,content;
        ConstraintLayout constraint;
        public ViewHolder(View itemView) {
            super(itemView);
            username = (TextView) itemView.findViewById(R.id.username);
            time = (TextView) itemView.findViewById(R.id.time);
            content = (TextView) itemView.findViewById(R.id.content);
            constraint=(ConstraintLayout) itemView.findViewById(R.id.constraint);
        }
    }
}
